package com.shujia.wyh.day13.studenttest2;

public class ConsumerThread extends Thread {
    private Student student;

    public ConsumerThread(Student student) {
        this.student = student;
    }

    @Override
    public void run() {
//        Student student = new Student();

        while (true) {
            synchronized (student) {
                //对于消费者而言，应该先看一看flag是否为true,如果不为true意味着数据还没有生产好，就应该等待
                if (!student.flag) {
                    try {
                        student.wait(); //阻塞，等生产者唤醒它
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println(student.name + "---" + student.age);
                //消费完数据后，将flag改成false
                student.flag = false;
                //消费者消费完数据后，应该通知生产者，生产数据
                student.notify();

            }
        }

    }
}
